package com.pronosticnews.client.admin.activity;

import org.atmosphere.extensions.gwt20.client.Atmosphere;
import org.atmosphere.extensions.gwt20.client.AtmosphereCloseHandler;
import org.atmosphere.extensions.gwt20.client.AtmosphereMessageHandler;
import org.atmosphere.extensions.gwt20.client.AtmosphereOpenHandler;
import org.atmosphere.extensions.gwt20.client.AtmosphereRequest;
import org.atmosphere.extensions.gwt20.client.AtmosphereRequestConfig;
import org.atmosphere.extensions.gwt20.client.AtmosphereResponse;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.pronosticnews.client.admin.ClientFactory;
import com.pronosticnews.client.admin.place.JourneePlace;
import com.pronosticnews.client.admin.view.listMessage.ListMessageView;
import com.pronosticnews.client.admin.view.listMessage.ListMessageView.Presenter;
import com.pronosticnews.client.util.messages.RPCEvent;
import com.pronosticnews.client.util.messages.RPCSerializer;

public class ListMessageActivity extends AbstractActivity implements Presenter {

	private ListMessageView view;

	private PlaceController placeController;
	private com.google.web.bindery.event.shared.EventBus eventBus;

	private AtmosphereRequest rpcRequest;

	public ListMessageActivity(JourneePlace place, ClientFactory clientFactory) {
		eventBus = clientFactory.getEventBus();
		view = clientFactory.getListMessageView();
		view.setPresenter(this);
		placeController = clientFactory.getPlaceController();
	}

	private void initMessagingConnection() {
		  RPCSerializer rpc_serializer = GWT.create(RPCSerializer.class);
	      AtmosphereRequestConfig rpcRequestConfig = AtmosphereRequestConfig.create(rpc_serializer);
	        rpcRequestConfig.setUrl(GWT.getModuleBaseURL() + "atmosphere/rpc");
	        rpcRequestConfig.setTransport(AtmosphereRequestConfig.Transport.STREAMING);
	        rpcRequestConfig.setFallbackTransport(AtmosphereRequestConfig.Transport.LONG_POLLING);
	        rpcRequestConfig.setOpenHandler(new AtmosphereOpenHandler() {
	            @Override
	            public void onOpen(AtmosphereResponse response) {
	               
	            }
	        });
	        rpcRequestConfig.setCloseHandler(new AtmosphereCloseHandler() {
	            @Override
	            public void onClose(AtmosphereResponse response) {
	               
	            }
	        });
	        rpcRequestConfig.setMessageHandler(new AtmosphereMessageHandler() {
	            @Override
	            public void onMessage(AtmosphereResponse response) {
	                RPCEvent event = (RPCEvent) response.getMessageObject();
	                if (event != null) {
	                    view.setMessage(event.getData());
	                }
	            }
	        });
	        Atmosphere atmosphere = Atmosphere.create();
	        rpcRequest = atmosphere.subscribe(rpcRequestConfig);
	}
	
	@Override
	public void start(AcceptsOneWidget container, EventBus eventBus) {
		container.setWidget(view.asWidget());
	}

	@Override
	public void pushMessage(String text) {
		RPCEvent myevent = new RPCEvent();
        myevent.setData(text);
        try {
			rpcRequest.push(myevent);
		} catch (SerializationException e) {
			e.printStackTrace();
		}
	}

	
	
}
